package org.example.service.Impl;

import lombok.AllArgsConstructor;
import org.example.dao.Dynamic;
import org.example.dao.User;
import org.example.enums.responseEnum;
import org.example.mapper.DynamicMapper;
import org.example.mapper.UserMapper;
import org.example.service.DynamicService;
import org.example.service.FileUploadService;
import org.example.util.MinioUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@AllArgsConstructor
public class FileUploadServiceImpl implements FileUploadService {

    private final MinioUtil minioUtil;

    private final UserMapper userMapper;

    private final DynamicService dynamicService;

    @Override
    public responseEnum uploadHeadImage(MultipartFile image, Integer id) {


        //判断文件大小是否超过2MB
        if (image.getSize() > (2 * 1024 * 1024)) {
            System.out.println("图片大小超过2MB");
            return responseEnum.error;
        }

        //将文件保存到minio中
        String fileName = minioUtil.setUserHeadImage(String.valueOf(id), image);

        //将头像信息保存在数据库中
        User user = new User();
        user.setId(id);
        user.setHeadImage(fileName);
        userMapper.updateUser(user);

        return responseEnum.success;
    }

    @Override
    public responseEnum uploadDynamicImage(MultipartFile images, Integer id) {
        //判断文件大小是否超过2MB
        if (images.getSize() > (2 * 1024 * 1024)) {
            System.out.println("图片大小超过2MB");
            return responseEnum.error;
        }

        //将图片存储进minio中
        String fileName = minioUtil.setDynamicImage(images);
        //将文件名存入数据库
        Dynamic dynamic = new Dynamic();
        dynamic.setId(id);
        dynamic.setImage(fileName);

        return dynamicService.addDynamicImage(dynamic);
    }
}
